#include "stdafx.h"

#include "GameState.h"
#include "InputManager.h"

#include "Components\GaulemRenderableComponent.h"

#include "Systems\GameStateRenderSystem.h"
#include "Systems\GaulemSystem.h"
#include "Systems\CameraSystem.h"
#include "Systems\BaseSystem.h"

#include "Time\TimeManager.h"
#include "Timer.h"

#include "TransformComponent.h"
#include "AnimatedComponent.h"
#include "BoundingBoxComponent.h"

#include "Utilities\CameraHelper.h"

#include "States\GameStateObject.h"

#include "World\World.h"

using namespace ZeEngine;
using namespace std;

GameState::GameState()
{
}

GameState::~GameState()
{
}

bool GameState::Init(GameEngine& gameEngine)
{   
    ResourceManager& resourceManager = gameEngine.GetResourceManager();

    //Load all resources for this state
    resourceManager.AddResourceFile("res/resources.xml");
    resourceManager.LoadResourceGroup("game_interface");
    resourceManager.LoadResourceGroup("test_sprites");
    resourceManager.LoadResourceGroup("temp_tiles");
    resourceManager.LoadResourceGroup("top");

    m_gameStateObject = make_shared<GameStateObject>();
    m_gameStateObject->Init(gameEngine);

    m_userInterface = make_unique<GameStateUserInterface>(gameEngine.GetGUISystem(), m_gameStateObject);
    m_userInterface->Init(gameEngine);

    AddSystem(make_unique<TransformSystem>());
    AddSystem(make_unique<AnimatedSystem>());
    AddSystem(make_unique<GaulemSystem>());
    AddSystem(make_unique<BaseSystem>(m_gameStateObject, gameEngine));
    AddSystem(make_unique<CameraSystem>());
    AddSystem(make_unique<GameStateRenderSystem>());

    InputManager::AddMouseListener(this, "GameStateMouseListener");

    return true;
}

void GameState::PreUpdate(ZeEngine::GameEngine& gameEngine) 
{
}

bool GameState::Update(ZeEngine::GameEngine& gameEngine)
{
    m_gameStateObject->Update(gameEngine);
    m_userInterface->Update(gameEngine);

    return true;
}

void GameState::PostUpdate(ZeEngine::GameEngine& gameEngine)
{
}

void GameState::Destroy(ZeEngine::GameEngine& gameEngine)
{
    ResourceManager& resourceManager = gameEngine.GetResourceManager();
    EntityManager& entityManager = gameEngine.GetEntityManager();

    resourceManager.UnloadResourceGroup("game_interface");

    entityManager.Clear();

    m_userInterface->Destroy(gameEngine);

    InputManager::RemoveMouseListener("GameStateMouseListener");
}

bool GameState::OnMouseMoved(const Vector2D<s32>& absolute, const Vector2D<s32>& relative)
{ 
    return false; 
}

bool GameState::OnMouseButtonPressed(const Vector2D<s32>& absolute, s32 button)
{
    return false; 
}

bool GameState::OnMouseButtonReleased(const Vector2D<s32>& absolute, s32 button)
{
    return false;
}

void GameState::StateIn()
{
    m_userInterface->Show();
}

void GameState::StateOut()
{
    m_userInterface->Hide();
}
